package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberMapper.add(member);
    }

    @Override
    public Map getMemberReport() throws Exception {

        //获取日历对象的实例
        Calendar calendar = Calendar.getInstance();
        //将日期往前倒推12个月,2019-03-26,2019-04-26,2019-05-26
        calendar.add(Calendar.MONTH,-12);

        //定义一个集合封装过去12个月的年月
        List<String> months = new ArrayList<>();
        //定义一个集合封装每一个月的会员数量
        List<Integer> memberCounts = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {

            //获取当前日期
            Date time = calendar.getTime();
            //获取过去12个月的数据 2019-03
            String month = DateUtils.parseDate2String(time, "yyyy-MM");
            months.add(month);

            //定义查询每个月会员数量的日期的起始值
            String dateBegin = month + "-01";
            //定义查询每个月会员数量的日期的结束值
            String dateEnd = month + "-31";

            Integer memberCount = memberMapper.findMemberCountByMonth(dateBegin,dateEnd);

            memberCounts.add(memberCount);

            calendar.add(Calendar.MONTH,+1);

        }

        //将数据封装到map中
        Map<String, List> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCounts",memberCounts);

        return map;
    }

    //查询性别占比
    @Override
    public Map getMemberSex() {
        List<Map> maps= memberMapper.querySex();
        List<Map> list = new ArrayList<>();
        List<String> list1 = new ArrayList<>();
        Long temp = 0L;
        for (Map map : maps) {
            String sex = (String) map.get("sex");
            Long count = (Long) map.get("counts");
            if(sex.equals("0")){
                map = new HashMap();
                map.put("value",count);
                map.put("name","女");
                list.add(map);
            }else if(sex.equals("1")){
                map = new HashMap();
                map.put("value",count);
                map.put("name","男");
                list.add(map);
            }else if(sex==null || sex.equals("")){
                temp += count;
            }else{
                temp += count;
            }
        }
        Map map1 = new HashMap();
        map1.put("value",temp);
        map1.put("name","未知");
        list.add(map1);

        list1.add("女");
        list1.add("男");
        list1.add("未知");

        Map map = new HashMap<>();
        map.put("memberSex",list1);
        map.put("memberCounts",list);
        return map;
    }

    //查询年龄占比
    @Override
    public Map getMemberAge() {
        Integer a=0,b=0,c=0,d=0;
        List<Integer> list1 = memberMapper.getMemberAge();

        for (Integer i : list1) {
            if(i>=0 && i<18){
                a++;
            }else if(i>=18 && i<30){
                b++;
            }else if(i>=30 && i<45){
                c++;
            }else {
                d++;
            }
        }
        List<String> list2 = new ArrayList<>();
        list2.add("0-18岁");
        list2.add("18-30岁");
        list2.add("30-45岁");
        list2.add("45岁以上");

        List<Map> list3 = new ArrayList<>();
        Map map1 = new HashMap<>();
        map1.put("value",a);
        map1.put("name","0-18岁");
        list3.add(map1);
        map1 = new HashMap<>();
        map1.put("value",b);
        map1.put("name","18-30岁");
        list3.add(map1);
        map1 = new HashMap<>();
        map1.put("value",c);
        map1.put("name","30-45岁");
        list3.add(map1);
        map1 = new HashMap<>();
        map1.put("value",d);
        map1.put("name","45岁以上");
        list3.add(map1);

        Map map = new HashMap();
        map.put("memberAge",list2);
        map.put("memberCounts",list3);
        return map;
    }

    @Override
    public Map getMemberReportByMonths(String start, String end) {
        ArrayList<Object> months = new ArrayList<>();
        ArrayList<Object> memberCounts = new ArrayList<>();
        List<Map<String,String>> list = memberMapper.getMemberReportByMonths(start,end);
        for (Map<String,String> stringMap : list){
            months.add(stringMap.get("months"));
            memberCounts.add(stringMap.get("memberCounts"));
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCounts",memberCounts);
        return map;
    }
}
